package leetcode;

import java.util.Deque;
import java.util.LinkedList;

/**
 * @program: datastructureandalogorithm
 * @description:
 * @author: hmx
 * @create: 2021-12-11 13:43
 **/
public class LeetCode42 {

    //双指针
    /*public int trap(int[] height) {
        int res = 0;
        int size = height.length;
        int left = 0;
        int right = size - 1;
        int leftMax = 0;
        int rightMax = 0;
        while (left < right) {
            leftMax = Math.max(leftMax, height[left]);
            rightMax = Math.max(rightMax, height[right]);
            if (leftMax < rightMax) {
                res += leftMax - height[left];
                left++;
            } else {
                res += rightMax - height[right];
                right--;
            }
        }
        return res;
    }*/

    //单调栈
    public int trap(int[] height) {
        int res = 0;
        int size = height.length;
        //单调栈,存储下标,下标递增,其对应的值递减
        Deque<Integer> stack = new LinkedList<>();
        for (int i = 0; i < size; ++i) {
            while (!stack.isEmpty() && height[i] > height[stack.peek()]) {
                int top = stack.pop();
                if (stack.isEmpty()) {
                    break;
                }
                int left = stack.peek();
                int width = i - left - 1;
                int h = Math.min(height[left], height[i]) - height[top];
                res += width * h;
            }

            stack.push(i);
        }
        return res;
    }

    //动态规划
    /*public int trap(int[] height) {
        int res = 0;
        int size = height.length;
        //leftMax[i]: <=i 的最大值
        int[] leftMax = new int[size];
        //rightMax[i]: >=i 的最大值
        int[] rightMax = new int[size];

        leftMax[0] = height[0];
        for (int i = 1; i < size - 1; i++) { //Search the left part for max bar size
            leftMax[i] = Math.max(leftMax[i - 1], height[i]);
        }

        rightMax[size - 1] = height[size - 1];
        for (int i = size - 2; i > 0; i--) { //Search the left part for max bar size
            rightMax[i] = Math.max(rightMax[i + 1], height[i]);
        }

        //计算雨水的单元格
        for (int i = 1; i < size - 1; i++) {
            res += Math.min(leftMax[i], rightMax[i]) - height[i];
        }
        return res;
    }*/

    //暴力
    /*public int trap(int[] height) {
        int res = 0;
        int size = height.length;
        for (int i = 1; i < size - 1; i++) {
            int maxLeft = 0;
            int maxRight = 0;
            //找出左侧最大值
            for (int j = i; j >= 0; j--) { //Search the left part for max bar size
                maxLeft = Math.max(maxLeft, height[j]);
            }
            //找出右侧最大值
            for (int j = i; j < size; j++) { //Search the right part for max bar size
                maxRight = Math.max(maxRight, height[j]);
            }
            //加上两侧最大之中的最小值 - 当前值,累积单元格的格数
            res += Math.min(maxLeft, maxRight) - height[i];
        }
        return res;
    }*/

    public static void main(String[] args) {
        LeetCode42 code = new LeetCode42();
        System.out.println("code.trap(new int[]{0,1,0,2,1,0,1,3,2,1,2,1}) = " + code.trap(new int[]{0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1}));
        System.out.println("code.trap(new int[]{4,2,0,3,2,5}) = " + code.trap(new int[]{4, 2, 0, 3, 2, 5}));
    }
}
